package springcloud.controller;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springcloud.service.PaymentService;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * (Payment)表控制层
 *
 * @author y
 * @since 2020-03-06 14:22:26
 */
@Slf4j
@RestController
@RequestMapping("payment")
public class PaymentController {
    @Value("${server.port}")
    private String port;

    @Resource
    private PaymentService paymentService;


    @GetMapping("/hystrix/info/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String s = paymentService.paymentInfo_OK(id);
        log.info(s);
        return s;
    }


    @GetMapping("/hystrix/info/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_ERROR_Handler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        String s = paymentService.paymentInfo_Error(id);
        log.info(s);
        return s;
    }

    public String paymentInfo_ERROR_Handler(@PathVariable("id") Integer id) {
        return "8001____paymentInfo_ERROR_Handler___异常返回";
    }


    //===服务熔断
    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String result = paymentService.paymentCircuitBreaker(id);
        log.info("*******result:"+result);
        return result;
    }





}